//
//  TempleViewController.m
//  ZiJinLian
//
//  Created by 🐛 on 2017/4/14.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "TempleViewController.h"
#import "InformationTableViewCell.h"
#import "PictureTableViewCell.h"
#import "FirstHeadView.h"
#import "SecondHeadView.h"
@interface TempleViewController ()<UITableViewDelegate,UITableViewDataSource,UIImagePickerControllerDelegate,UINavigationControllerDelegate,headViewDelegate,SecondHeadViewDelegate>
@property (nonatomic, strong) UITableView    * tableView;
@property (nonatomic, strong) NSArray        * name;
@property (nonatomic, strong) FirstHeadView  * headView;
@property (nonatomic, strong) SecondHeadView * SecondView;
@property (nonatomic, strong) NSArray        * array;
@property (nonatomic, strong) UIButton       * picBtn;
@property (strong, nonatomic) UITextField    * linkman;
@property (strong, nonatomic) UITextField    * numeber;


@end

@implementation TempleViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.name=[NSArray arrayWithObjects:@"联系人",@"联系电话", nil];
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, MainW, MainH-64) style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.backgroundColor=Gray_Color;
    tableView.dataSource = self;
    tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    tableView.separatorColor=SeparatorColor;
    tableView.separatorInset=UIEdgeInsetsZero;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    [tableView registerNib:[UINib nibWithNibName:@"InformationTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"InformationTableViewCell"];
    [tableView registerNib:[UINib nibWithNibName:@"PictureTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"PictureTableViewCell"];
}

#pragma mark -------- tableviewdelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 2;
    }else{
        return 1;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        return 50;
    }else {
        return 170;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 50;
    }else{
        return 46;
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        InformationTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"InformationTableViewCell"];
        if (!cell) {
            cell = [tableView dequeueReusableCellWithIdentifier:@"InformationTableViewCell"];
        }
        cell.titleName.text=self.name[indexPath.row];
        if (indexPath.row==0) {
            self.linkman=cell.textField;
        }else{
            self.numeber=cell.textField;
        }
        return cell;
    }else {
        PictureTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"PictureTableViewCell"];
        if (!cell) {
            cell = [tableView dequeueReusableCellWithIdentifier:@"PictureTableViewCell"];
        }
        [cell addSubview:[self creatBtn]];
        [cell.uploadPic addTarget:self action:@selector(changeIconImg:) forControlEvents:UIControlEventTouchUpInside];
        return cell;
    }    
    
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section==0) {
        self.headView=[[FirstHeadView alloc]init];
        self.headView.delegate=self;
        return self.headView;
    }else{
        self.SecondView=[[SecondHeadView alloc]init];
        self.SecondView.delegate=self;
        self.SecondView.view2.hidden=YES;
        self.SecondView.Cbtn.hidden=YES;
        return self.SecondView;
    }
}
-(UIButton *)creatBtn{
    UIButton *btn=[UIButton buttonWithType:0];
    btn.frame= CGRectMake(10, 120, MainW-20, 50);
    btn.enabled=YES;
    [btn setTitle:@"确定" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setBackgroundColor:Red_Color];
    [btn addTarget:self action:@selector(showClick) forControlEvents:UIControlEventTouchUpInside];
    ViewRadius(btn, 5);
    return btn;
}
-(void)showClick{
    [self showToast:@"提交成功!我们会在1-2个工作日内向您致电,请耐心等候!"];
     NSLog(@"确定 %@*****%@",self.linkman.text,self.numeber.text);
}
-(void)refreshLoadata:(UIButton *)sender{
    if (sender.tag==100) {
        self.SecondView.view.hidden=NO;
        self.SecondView.view2.hidden=YES;
        self.SecondView.Cbtn.hidden=YES;
    }else{
        self.SecondView.view.hidden=YES;
        self.SecondView.view2.hidden=NO;
        self.SecondView.Cbtn.hidden=NO;
    }
}
-(void)goHomeBtnClick{
    NSLog(@"选购");
}
#pragma mark ---------- 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
}

- (void)changeIconImg:(UIButton *)sender
{
    self.picBtn=sender;
    UIAlertController* alertC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    /*UIAlertControllerStyleActionSheet  警告框的样式:底部*/
    [alertC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    [alertC addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self takePhoto]; /*拍照*/
    }]];
    [alertC addAction:[UIAlertAction actionWithTitle:@"我的相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self LocalPhoto]; /*从相册*/
    }]];
    
    [self presentViewController:alertC animated:YES completion:nil];
}
/**
 * 从相册选择
 */
-(void)LocalPhoto
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    //资源类型为图片库
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    //设置选择后的图片可被编辑
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:nil];
}

/**
 * 拍照
 */
-(void)takePhoto
{
    //资源类型为照相机
    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
    //判断是否有相机
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])
    {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        //设置拍照后的图片可被编辑
        picker.allowsEditing = YES;
        //资源类型为照相机
        picker.sourceType = sourceType;
        [self presentViewController:picker animated:YES completion:nil];
    }else
    {
        [self showToast:@"该设备无摄像头"]; //跳出提示
    }
}
/**
 * 图像选取器的委托方法，选完图片后回调该方法
 */
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    //当图片不为空时显示图片并保存图片
    if (image!= nil)
    {
        [self.picBtn setBackgroundImage:image forState:UIControlStateNormal];
        //关闭相册界面
        [picker dismissViewControllerAnimated:YES completion:nil];
    }
    
}
// 相机选择取消按钮
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (void)changeFaceImg:(NSString *)userimg small:(NSString *)userlittleimg userid:(NSString *)userid
{
    
    
}


@end
